package com.quanquan.management.service;

import DTO.PageDTO;
import com.quanquan.management.core.service.GenericService;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.repository.SysUserRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.DateUtils;

import javax.annotation.PostConstruct;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author:lihonglinag
 * @date:2019-03-21 19:04
 * @version:v 1.0.0
 */
@Service
public class SysUserService extends GenericService<SysUser,String> {

    @Autowired
    private SysUserRepository sysUserRepository;

    @PostConstruct
    private void init(){
        setGenericRepository(sysUserRepository);
    }

    public SysUser getSysUserByUserName(String username){
        return sysUserRepository.findByUsername(username);
    }

    public PageDTO<SysUser> getPaged(PageRequestBody requestBody) {
        Integer size = null == requestBody.getSize() ? 10 : requestBody.getSize();
        Integer page = null == requestBody.getPage() ? 0 : requestBody.getPage();
        boolean asc = requestBody.isAsc();
        Sort sort =  new Sort(Sort.Direction.DESC, "lastLoginTime");
        String fromDate = requestBody.getFromDate();
        String toDate = requestBody.getToDate();
        String keyword = requestBody.getKeyword();
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<SysUser> paged = sysUserRepository.findAll((Specification<SysUser>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            // 创建具体查询条件
            if (StringUtils.isNotEmpty(fromDate)) {
                Date from = DateUtils.strToDate(fromDate, DateUtils.DATE);
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("regTime").as(Date.class), from));
            }
            if (StringUtils.isNotEmpty(toDate)) {
                Date to = DateUtils.getEndOfDay(DateUtils.strToDate(toDate, DateUtils.DATE));
                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("regTime").as(Date.class), to));
            }
            if (StringUtils.isNotEmpty(keyword)) {
                Predicate p1 = criteriaBuilder.like(root.get("username").as(String.class), "%" + keyword + "%");
                Predicate p2 = criteriaBuilder.or(criteriaBuilder.like(root.get("nickname").as(String.class), "%" + keyword + "%"),p1);
                Predicate p3 = criteriaBuilder.or(criteriaBuilder.like(root.get("phone").as(String.class), "%" + keyword + "%"),p2);
                Predicate p4 = criteriaBuilder.or(criteriaBuilder.like(root.get("email").as(String.class), "%" + keyword + "%"),p3);
                predicates.add(p4);
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }, pageable);
        PageDTO<SysUser> dto = new PageDTO<>();
        dto.setTotal(paged.getTotalElements());
        dto.setAsc(asc);
        dto.setFromDate(fromDate);
        dto.setToDate(toDate);
        dto.setKeyword(requestBody.getKeyword());
        dto.setList(paged.getContent());
        dto.setPage(page);
        dto.setPageSize(size);
        dto.setTotalPage(paged.getTotalPages());
        return dto;
    }

}
